﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CFSystem.JXC.BLL;
using CFSystem.JXC.Model;

namespace CFSystem.JXC.BasisInfo
{
    public partial class FrmStorageTransReasonSetting : Form
    {
        
        /// <summary>
        /// Edit Status
        /// </summary>
        private enum Operation
        {
            Adding,
            Deleting,
            Updating
        }

        Operation operation = Operation.Adding ;

        public FrmStorageTransReasonSetting()
        {
            InitializeComponent();
        }


        private void FrmStorageTransReasonSetting_Load(object sender, EventArgs e)
        {
            LoadAllInfo();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            SetEditStatus(false);
            operation = Operation.Adding;
            txtReason.BackColor = Color.Pink;
            txtReason.Focus();
        }

        /// <summary>
        /// 
        /// </summary>
        private void LoadAllInfo()
        {
            InvChgReasonSetting icrs = new InvChgReasonSetting();
            IList<InvTransReasonSettingInfo> itrsi = icrs.GetInTransReasonSettings();
            dgvStorageChgs.DataSource = itrsi;
            rdoInbound.Checked = true;
            SetEditStatus(true);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            
            switch (operation)
            {
                case Operation.Adding:
                    {
                        InvTransReasonSettingInfo itrsi = new InvTransReasonSettingInfo();
                        itrsi.InvTransReasonName = txtReason.Text.ToString().Trim();
                        if (rdoInbound.Checked)
                        {
                            itrsi.Type = "+";
                        }
                        else
                        {
                            itrsi.Type = "-";
                        }
                        InvChgReasonSetting icrs = new InvChgReasonSetting();
                        icrs.Insert(itrsi);
                        LoadAllInfo();
                        break;
                    }
                case Operation.Updating:
                    {
                        InvTransReasonSettingInfo itrsi = new InvTransReasonSettingInfo();
                        itrsi.InvTransReasonName = txtReason.Text.ToString().Trim();
                        itrsi.InvTransReasonID = Convert.ToInt32(dgvStorageChgs.SelectedRows[0].Cells[0].Value);
                        if (rdoInbound.Checked)
                        {
                            itrsi.Type = "+";
                        }
                        else
                        {
                            itrsi.Type = "-";
                        }
                        InvChgReasonSetting icrs = new InvChgReasonSetting();
                        icrs.Update(itrsi);
                        LoadAllInfo();
                        break;
                    }
                case Operation.Deleting:
                    {
                        InvChgReasonSetting icrs = new InvChgReasonSetting();
                        icrs.Delete(Convert.ToInt32(dgvStorageChgs.SelectedRows[0].Cells[0].Value));
                        LoadAllInfo();
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
            SetEditStatus(true);
            txtReason.Text = string.Empty;
            txtReason.BackColor = SystemColors.Window;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            SetEditStatus(false);
            operation = Operation.Updating;
            txtReason.Text = Convert.ToString(dgvStorageChgs.SelectedRows[0].Cells[1].Value);
            txtReason.BackColor = Color.Pink;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            SetEditStatus(false);
            operation = Operation.Deleting;
        }

        private void SetEditStatus(bool isEnabled)
        {
            btnAdd.Enabled = isEnabled;
            btnDelete.Enabled = isEnabled;
            btnUpdate.Enabled = isEnabled;
            btnSave.Enabled = !isEnabled;
            btnCancel.Enabled = !isEnabled;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            SetEditStatus(true);
            txtReason.Text = string.Empty;
            txtReason.BackColor = SystemColors.Window;
        }
    }
}
